#include "logger.h"
#include <algorithm>

namespace vlog
{

Logger::Logger(const std::string_view& logName) : m_logName(logName), m_logLevel(LogLevel::Level::INFO)
{
}

Logger::~Logger()
{
    ClearLogAppender();
}

void Logger::AddLogAppender(const LogAppender::ptr logAppender) noexcept
{
    m_logAppenders.push_back(logAppender);
}

void Logger::DelLogAppender(const LogAppender::ptr logAppender) noexcept
{
    auto it = std::find(m_logAppenders.begin(), m_logAppenders.end(), logAppender);
    if (it != m_logAppenders.end())
    {
        m_logAppenders.erase(it);
    }

    // for (auto it = m_logAppenders.begin(); it != m_logAppenders.end(); ++it)
    // {
    //     if (*it == logAppender)
    //     {
    //         m_logAppenders.erase(it);
    //         break;
    //     }
    // }
}

void Logger::ClearLogAppender() noexcept
{
    m_logAppenders.clear();
}

void Logger::WriteLog(event::LogEvent::ptr logEvent) noexcept
{
    if (logEvent->GetEventLogLevel() <= m_logLevel)
    {
        for (auto& i : m_logAppenders)
        {
            i->WriteLog(logEvent);
        }
    }
}

} // namespace vlog
